<template>
	<view class="container">
		
		<view class="goods">
			<image mode="aspectFill" :src="goodsInfo.pic"></u-image>
			<view class="content">
				<view class="name">
					<text>{{goodsInfo.goodsName}}</text>
				</view>
				<view class="price">
					<text>￥{{goodsInfo.price}}</text>
				</view>
			</view>
		</view>
		
		<view class="send-type">
			<view class="type ziqu" v-if="goodsInfo.sendType == 1">
				<view class="type-tag">收取方式: <u-tag class="tag" shape="square" text="自取" mode="dark" bg-color="#007aff" /></view>
				<u-line color="#f0f0f0" />
				<view>
					<u-form-item label="取货地址" label-width="150">
						<u-input type="text" v-model="goodsInfo.address" :disabled="true" placeholder="无"/>
					</u-form-item>
				</view>
			</view>
			<view class="type baosong" v-if="goodsInfo.sendType == 2">
				<view class="type-tag">收取方式: <u-tag class="tag" shape="square" text="包送" mode="dark" bg-color="#007aff" /></view>
				<u-line color="#f0f0f0" />
				<view>
					<u-form-item label="收货地址" label-width="150">
						<u-input type="text" v-model="goodsInfo.address" placeholder="例: 4栋501"/>
					</u-form-item>
				</view>
			</view>
		</view>
		
		<view class="bottom">
			<view>
				<text>实付款
					<text class="price">￥{{goodsInfo.price}}</text>
				</text>
			</view>
			<u-button 
				type="error" 
				shape="circle" 
				size="medium" 
				:loading="loading" 
				:custom-style="customStyle"
				@click="submit">确认购买</u-button>
		</view>
		
		<u-toast ref="uToast" />
		
	</view>
</template>

<script>
	import {
		createOrder
	} from '@/api/order.js'
	export default {
		data() {
			return {
				goodsInfo: {
					address: ''
				},
				loading: false,
				customStyle: {
					marginRight: '0rpx'
				}
			};
		},
		methods: {
			async submit() {
				
				if (this.goodsInfo.sendType == 2 && (this.goodsInfo.address == null || this.goodsInfo.address == '')) {
					this.$refs.uToast.show({
						title: '请填写收货地址',
						type: 'warning'
					})
				} else {
					
					this.loading = true
					
					let params = {}
					params.goodsId = this.goodsInfo.goodsId;
					params.goodsName = this.goodsInfo.goodsName;
					params.sellerId = this.goodsInfo.userId;
					params.price = this.goodsInfo.price;
					params.pic = this.goodsInfo.pic;
					params.sendType = this.goodsInfo.sendType;
					params.address = this.goodsInfo.address;
					await createOrder(params).then(res => {
						this.$u.route({
							url: '/pages/order-detail/order-detail',
							type: 'redirectTo',
							params: {
								orderId: res.data.orderId
							}
						})
					})
					
					this.loading = false
				}
				
				
				
			}
		},
		onLoad(options) {
			this.goodsInfo = JSON.parse(options.goodsInfo)
			console.log(this.goodsInfo)
		}
	}
</script>

<style lang="scss">
	@import "./create-order.scss";
</style>
